package com.kly.cashmall.widget.refresh;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;

import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;


/**
 * @author
 * @date 2018/1/22
 */
public class SimpleRefreshLayout extends SmartRefreshLayout {

  private boolean canLoadMore = false;
  private boolean canRefresh = true;

  private View refreshView;

  public SimpleRefreshLayout(Context context) {
    this(context, null);
  }

  public SimpleRefreshLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    setEnableLoadMore(canLoadMore);
    setEnableRefresh(canRefresh);
    setEnableAutoLoadMore(false);
  }

  @Override
  protected void onLayout(boolean changed, int l, int t, int r, int b) {
    super.onLayout(changed, l, t, r, b);
    judgeEnableLoadMore();
  }

  /**
   * 判断是否可以上来刷新
   */
  private void judgeEnableLoadMore() {
    View contentView = findRefreshView(mRefreshContent == null ? null : mRefreshContent.getView());
    if (contentView instanceof AdapterView) {
      Adapter adapter = ((AdapterView) contentView).getAdapter();
      super.setEnableLoadMore(canLoadMore && adapter != null && adapter.getCount() > 0);
    }

    if (contentView instanceof RecyclerView) {
      RecyclerView.Adapter adapter = ((RecyclerView) contentView).getAdapter();
      super.setEnableLoadMore(canLoadMore && adapter != null && adapter.getItemCount() > 0);
    }
  }

  /**
   * 在View内寻找AdapterView、RecyclerView
   */
  private View findRefreshView(View contentView) {
    if (refreshView != null) {
      return refreshView;
    }
    if (contentView == null) {
      return null;
    }
    if (viewFound(contentView)) {
      return contentView;
    }
    if (contentView instanceof ViewGroup) {
      for (int i = 0; i < ((ViewGroup) contentView).getChildCount(); i++) {
        View v = findRefreshView(((ViewGroup) contentView).getChildAt(i));
        if (viewFound(v)) {
          return refreshView = v;
        }
      }
    }
    return null;
  }

  private boolean viewFound(View view) {
    return view instanceof AdapterView || view instanceof RecyclerView;
  }

  @Override
  public SmartRefreshLayout setOnLoadMoreListener(OnLoadMoreListener listener) {
    setEnableLoadMore(listener != null);
    return (SmartRefreshLayout) super.setOnLoadMoreListener(listener);
  }

  @Override
  public SmartRefreshLayout setOnRefreshListener(OnRefreshListener listener) {
    setEnableRefresh(listener != null);
    return (SmartRefreshLayout) super.setOnRefreshListener(listener);
  }

  @Override
  public SmartRefreshLayout setEnableLoadMore(boolean enable) {
    super.setEnableLoadMore(canLoadMore = enable);
    judgeEnableLoadMore();
    return this;
  }

  @Override
  public SmartRefreshLayout setEnableRefresh(boolean enable) {
    super.setEnableRefresh(canRefresh = enable);
    return this;
  }

  /**
   * 没有更多状态
   */
  public SimpleRefreshLayout loadNoMore() {
    finishLoadMoreWithNoMoreData();
    return this;
  }


  /**
   * 强制调用刷新接口
   */
  public void callRefresh() {
    if (mRefreshListener != null) {
      mRefreshListener.onRefresh(this);
    }
  }

  /**
   * 强制调用加载更多接口
   */
  public void callLoadMore() {
    if (mLoadMoreListener != null) {
      mLoadMoreListener.onLoadMore(this);
    }
  }
}
